package com.ycwl.freetv.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbManager;
import android.util.Log;

public class DeviceReceiver extends BroadcastReceiver {
    static String TAG = "DeviceReceiver";
    private boolean isRegister = false;
    @Override
    public void onReceive(Context context, Intent intent) {
        switch (intent.getAction()) {
            case UsbManager.ACTION_USB_DEVICE_ATTACHED:
            {
                Log.d(TAG,"提示用户有新USB设备插入");
                break;
            }
            case UsbManager.ACTION_USB_DEVICE_DETACHED:
            {
                Log.d(TAG,"USB设备拔出");
                break;
            }
        }
    }
    public void register(Context context) {
        if (isRegister) return;
        IntentFilter filter = new IntentFilter();
        filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        context.registerReceiver(this, filter);
        isRegister = true;
    }
    public void unRegister(Context context) {
        if (!isRegister) return;
        isRegister = false;
        context.unregisterReceiver(this);
    }
}

